﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace UDS.Components
{
    /// <summary>
    /// 目录管理
    /// </summary>
    public class DirectoryClass
    {
        #region 目录添加

        /// <summary>
        /// 目录添加
        /// </summary>
        public void Add(int ParentID, string ProjectName, string ProjectRemark, string Wright, int Status, int Scale,
            DateTime StartDate, DateTime EndDate)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@ParentID", SqlDbType.Int, 20, ParentID),
                Database.MakeInParam("@ProjectName", SqlDbType.NVarChar, 20, ProjectName),
                Database.MakeInParam("@ProjectRemark", SqlDbType.NVarChar, 400, ProjectRemark),
                Database.MakeInParam("@Wright", SqlDbType.NVarChar, 40, Wright),
                Database.MakeInParam("@Status", SqlDbType.Int, 40, Status),
                Database.MakeInParam("@Scale", SqlDbType.Int, 20, Scale),
                Database.MakeInParam("@StartDate", SqlDbType.DateTime, 30, StartDate),
                Database.MakeInParam("@EndDate", SqlDbType.DateTime, 30, EndDate)
            };

            try
            {
                data.RunProc("dbo.sproc_AddProject", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目添加出错", ex);
            }
        }

        #endregion

        #region 获取某项目详细信息

        /// <summary>
        /// 获取某目录详细信息
        /// </summary>
        /// <param name="ClassID">目录ID</param>
        public SqlDataReader GetProjectDetail(int ClassID)
        {
            var data = new Database();
            SqlParameter[] parameters =
                {
                    Database.MakeInParam("@TeamID", SqlDbType.Int, 20, ClassID)
                };
            try
            {
                return data.RunDataReader("dbo.sproc_GetTeamInfo", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return null;
            }
        }

        #endregion

        #region 修改某目录信息

        /// <summary>
        /// 修改某目录信息
        /// </summary>
        /// <param name="ClassID">目录ID</param>
        public void Revise(int ClassID, string ProjectName, string ProjectRemark, int Status, int Scale, DateTime StartDate, DateTime EndDate)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@ProjectID", SqlDbType.Int, 20, ClassID),
                Database.MakeInParam("@ProjectName", SqlDbType.NVarChar, 20, ProjectName),
                Database.MakeInParam("@ProjectRemark", SqlDbType.NVarChar, 400, ProjectRemark),
                Database.MakeInParam("@Status", SqlDbType.Int, 40, Status),
                Database.MakeInParam("@Scale", SqlDbType.Int, 20, Scale),
                Database.MakeInParam("@StartDate", SqlDbType.DateTime, 30, StartDate),
                Database.MakeInParam("@EndDate", SqlDbType.DateTime, 30, EndDate)
            };
            try
            {
                data.RunProc("dbo.sproc_UpdateProject", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
            }
        }

        #endregion

        #region 删除某目录

        /// <summary>
        /// 删除某目录
        /// </summary>
        /// <param name="ClassID">项目ID</param>
        public void Delete(int ClassID)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TeamID", SqlDbType.Int, 20, ClassID),
            };
            try
            {
                data.RunProc("dbo.sproc_DeleteTeam", parameters);
            }

            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
            }
        }

        #endregion
    }
}